﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// The class ComplexPolynomialComponent represents a part of a complex polynomial.
    /// </summary>
    [Serializable]
    public class ComplexPolynomialComponent : IComplexPolynomialComponent
    {
        /// <summary>
        /// The coefficient of the component.
        /// </summary>
        private ComplexNumber coefficient;

        /// <summary>
        /// The exponent of the component.
        /// </summary>
        private double exponent;

        /// <summary>
        /// Initializes a new instance of the <see cref="ComplexPolynomialComponent"/> class.
        /// </summary>
        public ComplexPolynomialComponent()
        {
            this.coefficient = new ComplexNumber(1);
            this.exponent = 1;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ComplexPolynomialComponent"/> class.
        /// </summary>
        /// <param name="component">The component to clone.</param>
        public ComplexPolynomialComponent(ComplexPolynomialComponent component)
        {
            if (component == (ComplexPolynomialComponent) null)
            {
                throw new ArgumentNullException("component");
            }

            this.coefficient = component.Coefficient;
            this.exponent = component.Exponent;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ComplexPolynomialComponent"/> class.
        /// </summary>
        /// <param name="coefficient">The coefficient of the component.</param>
        public ComplexPolynomialComponent(ComplexNumber coefficient)
        {
            if (coefficient == (ComplexNumber) null)
            {
                throw new ArgumentNullException("coefficient");
            }

            this.coefficient = coefficient;
            this.exponent = 1;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ComplexPolynomialComponent"/> class.
        /// </summary>
        /// <param name="coefficient">The coefficient of the component.</param>
        /// <param name="exponent">The exponent of the component.</param>
        public ComplexPolynomialComponent(ComplexNumber coefficient, double exponent)
        {
            this.coefficient = coefficient;
            this.exponent = exponent;
        }

        /// <summary>
        /// Gets or sets the coefficient of the complex polynomial component.
        /// </summary>
        /// <value>The coefficient of the complex polynomial component.</value>
        public ComplexNumber Coefficient
        {
            get { return coefficient; }
            set { coefficient = value; }
        }

        /// <summary>
        /// Gets or sets the exponent of the complex polynomial component.
        /// </summary>
        /// <value>The exponent of the complex polynomial component.</value>
        public double Exponent
        {
            get { return exponent; }
            set { exponent = value; }
        }

        /// <summary>
        ///  Inverts the coefficients imaginary number part of the current complex polynomial component.
        /// </summary>
        public void InvertCoefficientImaginaryNumberPart()
        {
            this.coefficient.InvertImaginaryNumberPart();
        }

        /// <summary>
        /// Inverts the coefficients real number part of the current complex polynomial component.
        /// </summary>
        public void InvertCoefficientRealNumberPart()
        {
            this.coefficient.InvertRealNumberPart();
        }

        /// <summary>
        /// Inverts the coefficient of the current complex polynomial component.
        /// </summary>
        public void InvertCoefficient()
        {
            this.coefficient.InvertRealNumberPart();
            this.coefficient.InvertImaginaryNumberPart();
        }

        /// <summary>
        /// Inverts the value of the exponent.
        /// </summary>
        public void InvertExponent()
        {
            this.exponent = -this.exponent;
        }

        /// <summary>
        /// Adds a coefficient value from another ComplexPolynomialComponent to the current coefficient.
        /// </summary>
        /// <param name="add">The component to add.</param>
        public void AddCoefficient(ComplexPolynomialComponent add)
        {
            if (add == (ComplexPolynomialComponent)null)
                throw new ArgumentNullException("add");

            if (add.Coefficient == (ComplexNumber) null)
            {
                throw new ArgumentException("add");
            }

            this.coefficient += add.Coefficient;
        }

        /// <summary>
        /// Calculates the value of the current component with a specified value for x.
        /// </summary>
        /// <param name="x">The value for x.</param>
        /// <returns>The value of the current component with the specified value for x.</returns>
        public ComplexNumber CalulateValue(ComplexNumber x)
        {
            if (x == (ComplexNumber) null)
            {
                throw new ArgumentNullException("x");
            }

            ComplexNumber tempuri = x.Copy();

            tempuri.Pow(this.exponent);

            return this.coefficient * tempuri;
        }

        /// <summary>
        /// Calculates the value of the current component with a specified value for x.
        /// </summary>
        /// <param name="x">The value for x.</param>
        /// <returns>The value of the current component with the specified value for x.</returns>
        public ComplexNumber CalulateValue(double x)
        {
            ComplexNumber tempuri = new ComplexNumber(x);

            tempuri.Pow(this.exponent);

            return this.coefficient * tempuri;
        }

        /// <summary>
        /// Derivatives the current component.
        /// </summary>
        /// <returns>The derivative of the current component.</returns>
        public ComplexPolynomialComponent Derivative()
        {
            return new ComplexPolynomialComponent(this.coefficient * this.exponent, this.exponent - 1);
        }

        /// <summary>
        /// Copies the current instance.
        /// </summary>
        /// <returns>A copy of the current instance.</returns>
        public ComplexPolynomialComponent Copy()
        {
            return new ComplexPolynomialComponent(this.coefficient.Copy(), this.exponent);
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first complex polynomial component.</param>
        /// <param name="b">The second complex polynomial component.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(ComplexPolynomialComponent a, ComplexPolynomialComponent b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return ((a.Coefficient == b.Coefficient) && (a.Exponent == b.Exponent));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first complex polynomial component.</param>
        /// <param name="b">The second complex polynomial component.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(ComplexPolynomialComponent a, ComplexPolynomialComponent b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Implements the operator +.
        /// </summary>
        /// <param name="a">The first complex polynomial component.</param>
        /// <param name="b">The second complex polynomial component.</param>
        /// <returns>The result of the operator.</returns>
        public static ComplexPolynomialComponent operator +(ComplexPolynomialComponent a, ComplexPolynomialComponent b)
        {
            if (a == (ComplexPolynomialComponent) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (ComplexPolynomialComponent) null)
            {
                throw new ArgumentNullException("b");
            }

            if (a.Exponent != b.Exponent)
            {
                throw new IllegalArithmeticException("For a addition the exponents have to be even.");
            }

            return new ComplexPolynomialComponent(a.Coefficient + b.Coefficient, a.exponent);
        }

        /// <summary>
        /// Implements the operator -.
        /// </summary>
        /// <param name="a">The first complex polynomial component.</param>
        /// <param name="b">The second complex polynomial component.</param>
        /// <returns>The result of the operator.</returns>
        public static ComplexPolynomialComponent operator -(ComplexPolynomialComponent a, ComplexPolynomialComponent b)
        {
            if (a == (ComplexPolynomialComponent) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (ComplexPolynomialComponent) null)
            {
                throw new ArgumentNullException("b");
            }

            if (a.Exponent != b.Exponent)
            {
                throw new IllegalArithmeticException("For a subtraction the exponents have to be even.");
            }

            return new ComplexPolynomialComponent(a.Coefficient - b.Coefficient, a.exponent);
        }

        /// <summary>
        /// Implements the operator *.
        /// </summary>
        /// <param name="a">The first complex polynomial component.</param>
        /// <param name="b">The second complex polynomial component.</param>
        /// <returns>The result of the operator.</returns>
        public static ComplexPolynomialComponent operator *(ComplexPolynomialComponent a, ComplexPolynomialComponent b)
        {
            if (a == (ComplexPolynomialComponent) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (ComplexPolynomialComponent) null)
            {
                throw new ArgumentNullException("b");
            }

            return new ComplexPolynomialComponent(a.Coefficient * b.Coefficient, a.exponent + b.exponent);
        }

        /// <summary>
        /// Compares the current component with another one.
        /// </summary>
        /// <param name="compare">The component to compare.</param>
        /// <returns>True if the components are even otherwise, false.</returns>
        public bool CompareTo(ComplexPolynomialComponent compare)
        {
            if (compare == (ComplexPolynomialComponent) null)
            {
                throw new ArgumentNullException("compare");
            }

            return ((this.coefficient == compare.Coefficient) && (this.exponent == compare.Exponent));
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return "(" + this.coefficient + ")x^" + this.exponent;
        }
    }
}